package com.weixing.mall.provider.controller.admin;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.vo.GoodsPlaceVo;
import com.weixing.mall.provider.api.model.vo.ItemVo;
import com.weixing.mall.provider.model.dto.GoodsIdProductSkuDto;
import com.weixing.mall.provider.model.dto.ProductSpecDto;
import com.weixing.mall.provider.model.query.ProductQuery;
import com.weixing.mall.provider.service.AsyncService;
import com.weixing.mall.provider.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 货品管理Api
 *
 * @author  wx
 * @date  2019/10/24 10:57
 * @since  1.0
 */
@RequestMapping("product")
@RestController
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;
    @Autowired
    private AsyncService asyncService;

    /**
     * 列表查询
     *
     * @param isPage         是否分页
     * @param productQuery   查询参数
     * @return 结果
     */
    @GetMapping("")
    public Result list(Integer isPage, ProductQuery productQuery){
        if(null==isPage||isPage.equals(WhetherEnum.YES.getValue())){
            return ResultUtil.success(productService.selectProduct(initMpPage(),productQuery));
        }else{
            return ResultUtil.success(productService.selectProduct(productQuery));
        }

    }

    /**
     * 货品新增
     *
     * @param productSpecDto  新增参数
     * @return 结果
     */
    @PostMapping(value = "save")
    @NoRepeatSubmit
    public Result save(@RequestBody ProductSpecDto productSpecDto){
        /*List<ProductDto> productDtoList = productSpecDto.getProductDtoList();

        for (ProductDto productDto : productDtoList) {
            if(productDto.getId()==null){
                productDto.setProductSn( UUID.randomUUID().toString().replaceAll("-", ""));
            }

        }*/
        productService.save(productSpecDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "查询商品规格详情")
    @PostMapping("selectSpecByGoodsId/{goodsId}")
    public Result selectSpecByGoodsId(@PathVariable Long goodsId){
        return ResultUtil.success(productService.selectByGoodsId(goodsId));
    }


    @ApiOperation(value = "根据商品id和货品ksu查询货品Id")
    @PostMapping("selectProduct")
    public Result selectProduct(GoodsIdProductSkuDto goodsIdProductSkuDto){
        return ResultUtil.success(productService.selectProduct(goodsIdProductSkuDto));
    }

    /**
     * 根据商品款号来查询信息
     * @param productSn  货品吗
     * @return  结果
     */
    @GetMapping("findProductList/{productSn}")
    public Result<List<ItemVo>> findProductList(@PathVariable String productSn){
        return ResultUtil.success(productService.findProductList(productSn));
    }

    /**
     * 临时使用
     * @return
     */
    @GetMapping("tmp")
    @NoNeedAccess
    public Result tmp(){
        return ResultUtil.success(productService.linshi());
    }
    public static void main(String[] args) {
        System.out.println();
    }

    /**
     * 根据商品款号来查询商品id
     * @param productSn  货品吗
     * @return  结果
     */
    @GetMapping("findGoodsIdByProductSn/{productSn}")
    public Result<GoodsPlaceVo> findGoodsByProductSn(@PathVariable String productSn){
        return ResultUtil.success(productService.findGoodsByProductSn(productSn));
    }
    /**
     * 项目上线初始化图片搜索
     * @return
     */
    @GetMapping("initImag")
    @NoNeedAccess
    public Result initImag(){
  /*    for(int i=0;i<100;i++){
          asyncService.doSomething("index = " + i++,null);
      }*/
     //  return ResultUtil.success();
      return ResultUtil.success(productService.initImag());
    }

}
